/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import fr.cggveritas.crewintranet.jms.entities.CvMarque;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvModel;
import java.util.ArrayList;
import java.util.List;
import fr.cggveritas.crewintranet.jms.entities.CvVehicule;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvMarqueJpaController extends DaoImp implements Serializable {

    public CvMarqueJpaController() {
        super();
    }

    public void create(CvMarque cvMarque) throws PreexistingEntityException, Exception {
        if (cvMarque.getCvModelList() == null) {
            cvMarque.setCvModelList(new ArrayList<CvModel>());
        }
        if (cvMarque.getCvVehiculeList() == null) {
            cvMarque.setCvVehiculeList(new ArrayList<CvVehicule>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<CvModel> attachedCvModelList = new ArrayList<CvModel>();
            for (CvModel cvModelListCvModelToAttach : cvMarque.getCvModelList()) {
                cvModelListCvModelToAttach = em.getReference(cvModelListCvModelToAttach.getClass(), cvModelListCvModelToAttach.getIdmodel());
                attachedCvModelList.add(cvModelListCvModelToAttach);
            }
            cvMarque.setCvModelList(attachedCvModelList);
            List<CvVehicule> attachedCvVehiculeList = new ArrayList<CvVehicule>();
            for (CvVehicule cvVehiculeListCvVehiculeToAttach : cvMarque.getCvVehiculeList()) {
                cvVehiculeListCvVehiculeToAttach = em.getReference(cvVehiculeListCvVehiculeToAttach.getClass(), cvVehiculeListCvVehiculeToAttach.getIdvehicule());
                attachedCvVehiculeList.add(cvVehiculeListCvVehiculeToAttach);
            }
            cvMarque.setCvVehiculeList(attachedCvVehiculeList);
            em.persist(cvMarque);
            for (CvModel cvModelListCvModel : cvMarque.getCvModelList()) {
                CvMarque oldCvMarqueIdmarqueOfCvModelListCvModel = cvModelListCvModel.getCvMarqueIdmarque();
                cvModelListCvModel.setCvMarqueIdmarque(cvMarque);
                cvModelListCvModel = em.merge(cvModelListCvModel);
                if (oldCvMarqueIdmarqueOfCvModelListCvModel != null) {
                    oldCvMarqueIdmarqueOfCvModelListCvModel.getCvModelList().remove(cvModelListCvModel);
                    oldCvMarqueIdmarqueOfCvModelListCvModel = em.merge(oldCvMarqueIdmarqueOfCvModelListCvModel);
                }
            }
            for (CvVehicule cvVehiculeListCvVehicule : cvMarque.getCvVehiculeList()) {
                CvMarque oldCvMarqueIdmarqueOfCvVehiculeListCvVehicule = cvVehiculeListCvVehicule.getCvMarqueIdmarque();
                cvVehiculeListCvVehicule.setCvMarqueIdmarque(cvMarque);
                cvVehiculeListCvVehicule = em.merge(cvVehiculeListCvVehicule);
                if (oldCvMarqueIdmarqueOfCvVehiculeListCvVehicule != null) {
                    oldCvMarqueIdmarqueOfCvVehiculeListCvVehicule.getCvVehiculeList().remove(cvVehiculeListCvVehicule);
                    oldCvMarqueIdmarqueOfCvVehiculeListCvVehicule = em.merge(oldCvMarqueIdmarqueOfCvVehiculeListCvVehicule);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvMarque(cvMarque.getIdmarque()) != null) {
                throw new PreexistingEntityException("CvMarque " + cvMarque + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvMarque cvMarque) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvMarque persistentCvMarque = em.find(CvMarque.class, cvMarque.getIdmarque());
            List<CvModel> cvModelListOld = persistentCvMarque.getCvModelList();
            List<CvModel> cvModelListNew = cvMarque.getCvModelList();
            List<CvVehicule> cvVehiculeListOld = persistentCvMarque.getCvVehiculeList();
            List<CvVehicule> cvVehiculeListNew = cvMarque.getCvVehiculeList();
            List<String> illegalOrphanMessages = null;
            for (CvModel cvModelListOldCvModel : cvModelListOld) {
                if (!cvModelListNew.contains(cvModelListOldCvModel)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvModel " + cvModelListOldCvModel + " since its cvMarqueIdmarque field is not nullable.");
                }
            }
            for (CvVehicule cvVehiculeListOldCvVehicule : cvVehiculeListOld) {
                if (!cvVehiculeListNew.contains(cvVehiculeListOldCvVehicule)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvVehicule " + cvVehiculeListOldCvVehicule + " since its cvMarqueIdmarque field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<CvModel> attachedCvModelListNew = new ArrayList<CvModel>();
            for (CvModel cvModelListNewCvModelToAttach : cvModelListNew) {
                cvModelListNewCvModelToAttach = em.getReference(cvModelListNewCvModelToAttach.getClass(), cvModelListNewCvModelToAttach.getIdmodel());
                attachedCvModelListNew.add(cvModelListNewCvModelToAttach);
            }
            cvModelListNew = attachedCvModelListNew;
            cvMarque.setCvModelList(cvModelListNew);
            List<CvVehicule> attachedCvVehiculeListNew = new ArrayList<CvVehicule>();
            for (CvVehicule cvVehiculeListNewCvVehiculeToAttach : cvVehiculeListNew) {
                cvVehiculeListNewCvVehiculeToAttach = em.getReference(cvVehiculeListNewCvVehiculeToAttach.getClass(), cvVehiculeListNewCvVehiculeToAttach.getIdvehicule());
                attachedCvVehiculeListNew.add(cvVehiculeListNewCvVehiculeToAttach);
            }
            cvVehiculeListNew = attachedCvVehiculeListNew;
            cvMarque.setCvVehiculeList(cvVehiculeListNew);
            cvMarque = em.merge(cvMarque);
            for (CvModel cvModelListNewCvModel : cvModelListNew) {
                if (!cvModelListOld.contains(cvModelListNewCvModel)) {
                    CvMarque oldCvMarqueIdmarqueOfCvModelListNewCvModel = cvModelListNewCvModel.getCvMarqueIdmarque();
                    cvModelListNewCvModel.setCvMarqueIdmarque(cvMarque);
                    cvModelListNewCvModel = em.merge(cvModelListNewCvModel);
                    if (oldCvMarqueIdmarqueOfCvModelListNewCvModel != null && !oldCvMarqueIdmarqueOfCvModelListNewCvModel.equals(cvMarque)) {
                        oldCvMarqueIdmarqueOfCvModelListNewCvModel.getCvModelList().remove(cvModelListNewCvModel);
                        oldCvMarqueIdmarqueOfCvModelListNewCvModel = em.merge(oldCvMarqueIdmarqueOfCvModelListNewCvModel);
                    }
                }
            }
            for (CvVehicule cvVehiculeListNewCvVehicule : cvVehiculeListNew) {
                if (!cvVehiculeListOld.contains(cvVehiculeListNewCvVehicule)) {
                    CvMarque oldCvMarqueIdmarqueOfCvVehiculeListNewCvVehicule = cvVehiculeListNewCvVehicule.getCvMarqueIdmarque();
                    cvVehiculeListNewCvVehicule.setCvMarqueIdmarque(cvMarque);
                    cvVehiculeListNewCvVehicule = em.merge(cvVehiculeListNewCvVehicule);
                    if (oldCvMarqueIdmarqueOfCvVehiculeListNewCvVehicule != null && !oldCvMarqueIdmarqueOfCvVehiculeListNewCvVehicule.equals(cvMarque)) {
                        oldCvMarqueIdmarqueOfCvVehiculeListNewCvVehicule.getCvVehiculeList().remove(cvVehiculeListNewCvVehicule);
                        oldCvMarqueIdmarqueOfCvVehiculeListNewCvVehicule = em.merge(oldCvMarqueIdmarqueOfCvVehiculeListNewCvVehicule);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = cvMarque.getIdmarque();
                if (findCvMarque(id) == null) {
                    throw new NonexistentEntityException("The cvMarque with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvMarque cvMarque;
            try {
                cvMarque = em.getReference(CvMarque.class, id);
                cvMarque.getIdmarque();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvMarque with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CvModel> cvModelListOrphanCheck = cvMarque.getCvModelList();
            for (CvModel cvModelListOrphanCheckCvModel : cvModelListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvMarque (" + cvMarque + ") cannot be destroyed since the CvModel " + cvModelListOrphanCheckCvModel + " in its cvModelList field has a non-nullable cvMarqueIdmarque field.");
            }
            List<CvVehicule> cvVehiculeListOrphanCheck = cvMarque.getCvVehiculeList();
            for (CvVehicule cvVehiculeListOrphanCheckCvVehicule : cvVehiculeListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvMarque (" + cvMarque + ") cannot be destroyed since the CvVehicule " + cvVehiculeListOrphanCheckCvVehicule + " in its cvVehiculeList field has a non-nullable cvMarqueIdmarque field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(cvMarque);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvMarque> findCvMarqueEntities() {
        return findCvMarqueEntities(true, -1, -1);
    }

    public List<CvMarque> findCvMarqueEntities(int maxResults, int firstResult) {
        return findCvMarqueEntities(false, maxResults, firstResult);
    }

    private List<CvMarque> findCvMarqueEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvMarque.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvMarque findCvMarque(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvMarque.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvMarqueCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvMarque> rt = cq.from(CvMarque.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
    
    
}
